// jWMI.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

#include <mes_wmi_query_jWMI.h>
#include "WMITESTS.cpp"

std::string Java_To_WStr(JNIEnv *env, jstring string)
{
    std::string value;

	jboolean isCopy = true;

    const jchar *raw = env->GetStringChars(string, &isCopy);
    jsize len = env->GetStringLength(string);
    const jchar *temp = raw;

    value.assign(raw, raw + len);

    env->ReleaseStringChars(string, raw);

    return value;
}

JNIEXPORT void JNICALL Java_mes_wmi_query_jWMI_initWMI
  (JNIEnv * env, jobject obj){
	  initWMI();
}

JNIEXPORT void JNICALL Java_mes_wmi_query_jWMI_release
  (JNIEnv * env, jobject obj){
	  release();
}

JNIEXPORT jstring JNICALL Java_mes_wmi_query_jWMI_ExecQuery
  (JNIEnv * env, jobject obj, jstring str, jstring comma){
	  std::string conv = Java_To_WStr(env,str);
	  std::string convComma = Java_To_WStr(env,comma);
	  std::string retValue = ExecQuerySync(conv,convComma);
	  return env->NewStringUTF(retValue.c_str());
}


JNIEXPORT jstring JNICALL Java_mes_wmi_query_jWMI_ExecMultiQuery
  (JNIEnv * env, jobject obj, jobjectArray objArray, jobjectArray objArrayComma){
	  int i = 0;
	  std::vector<std::string> vect;
	  std::vector<std::string> vectComma;
	  for(;i < env->GetArrayLength(objArray); i++){
		  jstring str = (jstring)env->GetObjectArrayElement(objArray,i);
		  jstring strComma = (jstring)env->GetObjectArrayElement(objArrayComma,i);
		  vect.push_back(Java_To_WStr(env,str));
		  vectComma.push_back(Java_To_WStr(env,strComma));
		  
	  }
	  std::string rezult = ExecMultiQuery(vect,vectComma);

	  return env->NewStringUTF(rezult.c_str());
}

